<?php

require_once($_SERVER['DOCUMENT_ROOT'] . "/defaultconf.mex");
MYTCONN();
include($GLOBALS['OBJ'] . 'obj.mathgrids.mex');
$QCORR = '';
$QMVND = '';
$QMCNL = '';
if (isset($_POST['QrCorresponsal']) && $_POST['QrCorresponsal'] != '') {
    $QCORR = " AND Op_Corr IN ({$_POST['QrCorresponsal']}) ";
}
if (!isset($_POST['QGroup'])) {
    $_POST['QGroup'] = 'DATE(Op_Fecha),Op_Corr';
}
if (!isset($_POST['QGroup'])) {
    $_POST['QGroup'] = 'DATE(Op_Fecha),Op_Corr';
}
if (isset($_POST['Op_MonDPago']) && $_POST['Op_MonDPago'] > 0) {
    $QMVND = ' AND Op_MonDPago = '.$_POST['Op_MonDPago'];
}
if (isset($_POST['Op_Modo']) && $_POST['Op_Modo'] > 0) {
    $QMCNL = ' AND Op_Modo = '.$_POST['Op_Modo'];
}
if (!isset($_POST['TStart'])) {
    $_POST['TStart'] = date("Y-m-01");
    $_POST['TEnd'] = date("Y-m-d");
}

$tableheader = '<table width="100%"  border="0">
      <tr height="20" align="center">
	<td NOWRAP width="6%">Ordem</td>
        <td NOWRAP width="8%">Data</td>
        <td NOWRAP width="6%">Banco</td>
        <td NOWRAP width="5%">#</td>
        <td NOWRAP width="13%"><b>Remessa Cliente</b></td>
        <td NOWRAP width="5%">C. Balcão</td>
        <td NOWRAP width="12%"><b>Resultado</b></td>
        <td NOWRAP width="5%">C. Negociado</td>
        <td NOWRAP width="12%"><b>Resultado</b></td>
	<td NOWRAP width="9%">Co. Money</td>
	<td NOWRAP width="9%">Co. Corr</td>
	<td NOWRAP width="9%">Imp. Selo</td>
        <td NOWRAP width="9%">Margem Venda</td>
        <td NOWRAP width="12%">Venda</td>
        <td NOWRAP width="9%">Taxas</td>
        <td NOWRAP width="9%"><b>Margem Lucro</b></td>
        <td NOWRAP width="12%">Lucro</td>
      </tr>
	  <tr><td colspan="14"><hr width="100%" size="1" /></td></tr>';

echo '<div class="Line">Totais de OperaÇ&otilde;es</div>';
echo $tableheader;

## Set Fields
$SentAmount = 'IF(FraModo=1,FraEnviado,FraARecibir)';
$PaidAmount = 'IF(FraModo=1,FraARecibir*CorrRate,FraARecibir)';
$FeesAmount = 'IF(FraModo=1,FraCargo,FraIncComisionCo)';

$Results = $GLOBALS['MYT']->db_array("SELECT
	FraMonDPago,
	FraMonCobro,
	CONCAT(MONTH(Op_Fecha),'-',YEAR(Op_Fecha)) AS Op_Fecha,
	Op_Corr,
	SUM($SentAmount) AS SentAmount,
	SUM($PaidAmount) AS PaidAmount,
	SUM($PaidAmount/{$_POST['RATETYPE']}) AS PaidCost,
	Op_Orden,
	SUM($FeesAmount) AS Fee,
	SUM(IF(FraStatus IN (10,21),-1,1)) AS CountOr,
	CorrAlias
	FROM ordenespago
	LEFT JOIN ordenesfinanceiro ON Of_Orden = Op_Orden
	LEFT JOIN facturas_income ON FraIncOrdenPago = Op_Orden
        INNER JOIN facturas ON FraOrdenPago = Op_Orden OR FraID = FraIncID
        INNER JOIN corresponsales ON CorrNumero = Op_Corr
	WHERE DATE(Op_Fecha) BETWEEN '{$_POST['TStart']}' AND '{$_POST['TEnd']}'
	AND FraStatus IN (7,8,9,10,11,21)
	$QCORR $QMCNL $QMVND
	GROUP BY Op_Corr,FraMonDPago,FraMonCobro");
for ($h = 1; $h <= $Results[0]; $h++) {

    $DMEAMOUNT = 1;
    $DMOAMOUNT = 1;

    if ($Results[$h]['SentAmount'] > 0) {
        $DMEAMOUNT = $Results[$h]['SentAmount'];
        $DMOAMOUNT = $Results[$h]['PaidCost'];
    }
    $RateIN = $Results[$h]['PaidAmount'] / $DMEAMOUNT;
    if ($DMOAMOUNT > 0) {
        $RateOUT = $Results[$h]['PaidAmount'] / $DMOAMOUNT;
    } else {
        $RateOUT = 0;
    }
    $ProfitAmount = $Results[$h]['SentAmount'] - $Results[$h]['PaidCost'];
    if (FieldResult($Results, $h, 'Op_Corr') == 307) {
        $REL = FieldResult($Results, $h, 'SentAmount') / 100;
        $Results[$h]['imp'] = $REL * 0.12;
        $Results[$h]['COMM'] = FieldResult($Results, $h, 'Fee') - FieldResult($Results, $h, 'imp');
        $Results[$h]['COMM1'] = ((FieldResult($Results, $h, 'COMM') / 3) * 1);
        $Results[$h]['Fee'] = FieldResult($Results, $h, 'COMM1');
        $Results[$h]['COMM2'] = ((FieldResult($Results, $h, 'COMM') / 3) * 2);
    } else {
        $REL = FieldResult($Results, $h, 'SentAmount') / 100;
        $Results[$h]['imp'] = '';
        $Results[$h]['COMM'] = '';
        $Results[$h]['COMM1'] = '';
        $Results[$h]['COMM2'] = '';
    }

    echo '<tr bgcolor="' . gridlines($h) . '" height="20">
	<td NOWRAP></td>
    <td NOWRAP>' . $Results[$h]['Op_Fecha'] . '</td>
    <td NOWRAP>' . $Results[$h]['CorrAlias'] . '</td>
	<td NOWRAP align="right">' . $Results[$h]['CountOr'] . '</td>
    <td NOWRAP align="right"><b>' . number_format($Results[$h]['SentAmount'] . "1", 2) . ' ' . $_SESSION[$SESSST . 'CURS'][$Results[$h]['FraMonCobro']]['CurLabel'] . '</b></td>
    <td NOWRAP align="right">' . number_format($RateIN, 9) . '</td>
	<td NOWRAP align="right"><b>' . number_format($Results[$h]['PaidAmount'], 2) . ' ' . $_SESSION[$SESSST . 'CURS'][$Results[$h]['FraMonDPago']]['CurLabel'] . '</b></td>
	<td NOWRAP align="right">' . number_format($RateOUT, 9) . '</td>
	<td NOWRAP align="right"><b>' . number_format($Results[$h]['PaidCost'], 2) . ' ' . $_SESSION[$SESSST . 'CURS'][$Results[$h]['FraMonCobro']]['CurLabel'] . '</b></td>
	<td NOWRAP align="right">' . number_format(FieldResult($Results, $h, 'COMM1'), 2) . ' ' . $_SESSION[$SESSST . 'CURS'][$Results[$h]['FraMonCobro']]['CurLabel'] . '</td>
	<td NOWRAP align="right">' . number_format(FieldResult($Results, $h, 'COMM2'), 2) . ' ' . $_SESSION[$SESSST . 'CURS'][$Results[$h]['FraMonCobro']]['CurLabel'] . '</td>
	<td NOWRAP align="right">' . number_format(FieldResult($Results, $h, 'imp'), 2) . ' ' . $_SESSION[$SESSST . 'CURS'][$Results[$h]['FraMonCobro']]['CurLabel'] . '</td>
	<td NOWRAP align="right">' . number_format(($ProfitAmount * 100) / $DMEAMOUNT, 9) . "%" . '</td>
	<td NOWRAP align="right">' . number_format($ProfitAmount, 2) . ' ' . $_SESSION[$SESSST . 'CURS'][$Results[$h]['FraMonCobro']]['CurLabel'] . '</td>
	<td NOWRAP align="right">' . number_format(FieldResult($Results, $h, 'Fee'), 2) . ' ' . $_SESSION[$SESSST . 'CURS'][$Results[$h]['FraMonCobro']]['CurLabel'] . '</td>';
    if (FieldResult($Results, $h, 'Op_Corr') == 307) {
        $ProfitAmount += $Results[$h]['COMM1'];
    } else {
        $ProfitAmount += FieldResult($Results, $h, 'Fee');
    }
    echo '<td NOWRAP align="right"><b>' . number_format(($ProfitAmount * 100) / $DMEAMOUNT, 9) . "%" . '</b></td>
	<td NOWRAP align="right">' . number_format($ProfitAmount, 2) . ' ' . $_SESSION[$SESSST . 'CURS'][$Results[$h]['FraMonCobro']]['CurLabel'] . '</td>
	</tr>';
}
echo '</table>
    <div class="Line">Consulta de OperaÇ&otilde;es</div>';
echo $tableheader;

$Results = $GLOBALS['MYT']->db_array("SELECT
	FraMonDPago,
	FraMonCobro,
	IF('{$_POST['QGroup']}'='Op_Orden',Op_Fecha,DATE(Op_Fecha)) AS Op_Fecha,
	Op_Corr,
	SUM($SentAmount) AS SentAmount,
	SUM($PaidAmount) AS PaidAmount,
	IF({$_POST['RATETYPE']} IS NULL,0,1) AS Evaluate,
	SUM(IF({$_POST['RATETYPE']} IS NULL,0,(FraARecibir*CorrRate)/{$_POST['RATETYPE']})) AS PaidCost,
	Op_Orden,
	SUM($FeesAmount) AS Fee,
	SUM(IF(FraStatus IN (10,21),-1,1)) AS CountOr,
	CorrAlias,
	CorrNumero
	FROM ordenespago
	LEFT JOIN ordenesfinanceiro ON Of_Orden = Op_Orden
	LEFT JOIN facturas_income ON FraIncOrdenPago = Op_Orden
        INNER JOIN facturas ON FraOrdenPago = Op_Orden OR FraID = FraIncID
        INNER JOIN corresponsales ON CorrNumero = Op_Corr
	WHERE DATE(Op_Fecha) BETWEEN '{$_POST['TStart']}' AND '{$_POST['TEnd']}'
	AND FraStatus IN (7,8,9,10,11,21)
	$QCORR $QMCNL $QMVND
	GROUP BY Evaluate,{$_POST['QGroup']},FraMonDPago,FraMonCobro ORDER BY Op_Orden");
for ($h = 1; $h <= $Results[0]; $h++) {

    $DMEAMOUNT = 1;
    $DMOAMOUNT = 1;

    if ($Results[$h]['SentAmount'] > 0) {
        $DMEAMOUNT = $Results[$h]['SentAmount'];
        $DMOAMOUNT = $Results[$h]['PaidCost'];
    }

    $RateIN = $Results[$h]['PaidAmount'] / $DMEAMOUNT;
    if ($DMOAMOUNT > 0) {
        $RateOUT = $Results[$h]['PaidAmount'] / $DMOAMOUNT;
    } else {
        $RateOUT = 0;
    }
    if ($DMOAMOUNT > 0) {
        $ProfitAmount = $Results[$h]['SentAmount'] - $Results[$h]['PaidCost'];
    } else {
        $ProfitAmount = 0;
    }
    if (FieldResult($Results, $h, 'Op_Corr') == 307) {
        $REL = FieldResult($Results, $h, 'SentAmount') / 100;
        $Results[$h]['imp'] = $REL * 0.12;
        $Results[$h]['COMM'] = FieldResult($Results, $h, 'Fee') - FieldResult($Results, $h, 'imp');
        $Results[$h]['COMM1'] = ((FieldResult($Results, $h, 'COMM') / 3) * 1);
        $Results[$h]['Fee'] = FieldResult($Results, $h, 'COMM1');
        $Results[$h]['COMM2'] = ((FieldResult($Results, $h, 'COMM') / 3) * 2);
    } else {
        $REL = FieldResult($Results, $h, 'SentAmount') / 100;
        $Results[$h]['imp'] = '';
        $Results[$h]['COMM'] = '';
        $Results[$h]['COMM1'] = '';
        $Results[$h]['COMM2'] = '';
    }

    echo '<tr bgcolor="' . gridlines($h) . '" height="20">
    <td NOWRAP>' . $Results[$h]['Op_Orden'] . '</td>
    <td NOWRAP>' . $Results[$h]['Op_Fecha'] . '</td>
    <td NOWRAP>' . $Results[$h]['CorrAlias'] . '</td>
	<td NOWRAP align="right">' . $Results[$h]['CountOr'] . '</td>
	<td NOWRAP align="right"><b>' . number_format($Results[$h]['SentAmount'] . "1", 2) . ' ' . $_SESSION[$SESSST . 'CURS'][$Results[$h]['FraMonCobro']]['CurLabel'] . '</b></td>
	<td NOWRAP align="right">' . number_format($RateIN, 9) . '</td>
	<td NOWRAP align="right"><b>' . number_format($Results[$h]['PaidAmount'], 2) . ' ' . $_SESSION[$SESSST . 'CURS'][$Results[$h]['FraMonDPago']]['CurLabel'] . '</b></td>
	<td NOWRAP align="right">';
    mn_field('text', 'ED', $_POST['RATETYPE'] . $h, number_format($RateOUT, 9), '8', '8', '', 'INPNUM');
    mn_field('button', 'ED', 'of_Set', 'SET', '', '', '', '" onclick="replaceUrl(\'rsApply\',\'_response/rsproxy.php?pr=fn_payorderclose&SetOf=' . $Results[$h]['Op_Orden'] . '&COR=' . $Results[$h]['CorrNumero'] . '&RTE=\'+document.getElementById(\'' . $_POST['RATETYPE'] . $h . '\').value);');
    echo '</td>
	<td NOWRAP align="right"><b>' . number_format($Results[$h]['PaidCost'], 2) . ' ' . $_SESSION[$SESSST . 'CURS'][$Results[$h]['FraMonCobro']]['CurLabel'] . '</b></td>
	<td NOWRAP align="right">' . number_format(FieldResult($Results, $h, 'COMM1'), 2) . ' ' . $_SESSION[$SESSST . 'CURS'][$Results[$h]['FraMonCobro']]['CurLabel'] . '</td>
	<td NOWRAP align="right">' . number_format(FieldResult($Results, $h, 'COMM2'), 2) . ' ' . $_SESSION[$SESSST . 'CURS'][$Results[$h]['FraMonCobro']]['CurLabel'] . '</td>
	<td NOWRAP align="right">' . number_format(FieldResult($Results, $h, 'imp'), 2) . ' ' . $_SESSION[$SESSST . 'CURS'][$Results[$h]['FraMonCobro']]['CurLabel'] . '</td>
	<td NOWRAP align="right">' . number_format(($ProfitAmount * 100) / $DMEAMOUNT, 9) . "%" . '</b></td>
	<td NOWRAP align="right">' . number_format($ProfitAmount, 2) . ' ' . $_SESSION[$SESSST . 'CURS'][$Results[$h]['FraMonCobro']]['CurLabel'] . '</td>
	<td NOWRAP align="right">' . number_format(FieldResult($Results, $h, 'Fee'), 2) . ' ' . $_SESSION[$SESSST . 'CURS'][$Results[$h]['FraMonCobro']]['CurLabel'] . '</td>';
    if (FieldResult($Results, $h, 'Op_Corr') == 307) {
        $ProfitAmount += $Results[$h]['COMM1'];
    } else {
        $ProfitAmount += FieldResult($Results, $h, 'Fee');
    }
    echo '<td NOWRAP align="right"><b>' . number_format(($ProfitAmount * 100) / $DMEAMOUNT, 9) . "%" . '</b></td>
	<td NOWRAP align="right">' . number_format($ProfitAmount, 2) . ' ' . $_SESSION[$SESSST . 'CURS'][$Results[$h]['FraMonCobro']]['CurLabel'] . '</td>
	</tr>';
}
echo '</table>';